/** 创建组件对象 * */
var indicatorExtractionForm = new Form();
var indicatorExtractionTable = new Table();
var indicatorExtractionPage = new Page();
var indicatorExtractionDialog = new Dialog();
var indicatorExtractionAjax = new Ajax();
var array = [ "indicatorExtractionQueryDiv", "indicatorExtractionButtonDiv", "indicatorExtractionPageDiv" ];

var companyObj;
var unitObj;

/** 页面加载完执行 * */
$(function() {
	initScript();
	initConditions();
	initButtons();
	initPage();
	pageQuery();
	resetTableHeight("indicatorExtractionTableDiv", array);
	initElementsControl();
});

/** 初始化script * */
function initScript() {
	loadScript(appPath
			+ "/project-yddemo/indicatorDataExtraction/locale/IndicatorExtraction_"
			+ appLocale + ".js"); // 1.加载国际化js*
	loadScript(appPath
			+ "/project-yddemo/indicatorDataExtraction/properties/IndicatorExtractionProperties_View.js"); // 2.加载配置js(非必配,配置也可写在本js中)
}

/** 初始化查询条件 * */
function initConditions() {
	indicatorExtractionForm.initTableConditions({
		divId : "indicatorExtractionQueryDiv", // 1.归属div*
		type : "table",
		conditions : IndicatorExtractionProperties_View.getViewConditions(), // 2.配置条件*
		button : { // 4.操作按钮*
			width : "80", // 占用宽度*
			align : "center", // left,center,right,百分比*
			label : indicatorExtractionLocale.text.search, // 文本*
			action : "yddemo:indicatorDataExtraction:view",// 增加页面元素控制
			clickFun : function() { // 按钮事件*
				var data = indicatorExtractionForm.getQueryConditions();
				if(companyObj!=undefined){
					data.companyId = companyObj.id;
				}
				if(unitObj!=undefined){
					data.unitId = unitObj.id;
				}
				indicatorExtractionTable.reloadData(data);
			}
		}
	});
	
	var unitInput = $("[key$='unitId']");
	unitInput.bind("click", function() {
		if(companyObj==undefined){
			indicatorExtractionDialog.alertDialog(indicatorExtractionLocale.text.selectCompany);
			return false;
		}
		sessionStorage.selectedId = companyObj.id;
		indicatorExtractionDialog.iframeDialog(requestPath
				+ "/common/comUnits/dialog/comUnitDialog",
				indicatorExtractionLocale.field.name, 400, 300,
				"yddemo:indicatorDataExtraction:view", false);
	});
	unitInput.bind("focus",function(){
		unitInput.blur();
	});
	
	var companyInput = $("[key$='companyId']");
	companyInput.bind("click", function() {
		indicatorExtractionDialog.iframeDialog(requestPath
				+ "/yddemo/indicatorDataExtractions/dialog/companyDialog",
				indicatorExtractionLocale.field.company, 400, 300,
				"yddemo:indicatorDataExtraction:view", false);
	});
	companyInput.bind("focus",function(){
		companyInput.blur();
	});
}

/** 初始化按钮 * */
function initButtons() {
	indicatorExtractionTable.initButtons({
		divId : "indicatorExtractionButtonDiv", // 1.归属div*
		buttons : IndicatorExtractionProperties_View.getViewButtons()
	// 2.按钮组配置*
	});
	disableButton();
}

/** 初始化分页 * */
function initPage() {
	indicatorExtractionPage.initPage({
		divId : "indicatorExtractionPageDiv", // 1.归属div*
		eachPageNum : 10, // 2.每页条数(5,10,15,20)*
		pageChangeFun : function() { // 3.分页刷新事件*
			indicatorExtractionTable.reloadData();
		}
	});
}

/** 分页查询 * */
function pageQuery() {
	var data = indicatorExtractionForm.getQueryConditions();
	indicatorExtractionTable.initTable({
		divId : "indicatorExtractionTableDiv", // 1.归属div*
		dataAjax : { // 2.发送ajax请求*
			url : requestPath + "/yddemo/indicatorDataExtractions", // 请求url*
			type : "GET", // 请求类型*
			data : data, // 请求参数*
			page : indicatorExtractionPage
		// 分页对象(需要分页时配置)
		},
		columns : IndicatorExtractionProperties_View.getViewColumns(),// 3.表格列配置
		rowClickFun : function(data) {
			if (sessionStorage.orgCode != "*") {
				btnStatus();
			}
		}
	});
	indicatorExtractionTable.reloadData();
}

function btnStatus() {
	$("[action='yddemo:indicatorDataExtraction:edit']").unbind();
	$("[action='yddemo:indicatorDataExtraction:delete']").unbind();
	if (indicatorExtractionTable.getSelectDatas().length > 0) {
		setElementsEnabled("yddemo:indicatorDataExtraction:delete", "button", true);
		if (indicatorExtractionTable.getSelectDatas().length != 1) {
			setElementsEnabled("yddemo:indicatorDataExtraction:edit", "button", false);
		} else {
			setElementsEnabled("yddemo:indicatorDataExtraction:edit", "button", true);
		}
	} else {
		disableButton();
	}
	$("[action='yddemo:indicatorDataExtraction:edit']").bind("click", function() {
		updateIndicatorDataExtraction();
	});
	$("[action='yddemo:indicatorDataExtraction:delete']").bind("click", function() {
		deleteIndicatorDataExtraction();
	});
}

function disableButton() {
	if (sessionStorage.orgCode == "*") {
		setElementsEnabled("yddemo:indicatorDataExtraction:add", "button", false);
	}
	setElementsEnabled("yddemo:indicatorDataExtraction:edit", "button", false);
	setElementsEnabled("yddemo:indicatorDataExtraction:delete", "button", false);
}

/** 新增 * */
function addIndicatorDataExtraction() {
	indicatorExtractionDialog.iframeDialog(requestPath + "/yddemo/indicatorDataExtractions/add",
			indicatorExtractionLocale.text.add, 450, 340, "yddemo:indicatorDataExtraction:add", false);
}

/** 修改 * */
function updateIndicatorDataExtraction() {
	var selectDatas = indicatorExtractionTable.getSelectDatas();
	if ($("[action='yddemo:indicatorDataExtraction:edit']").hasClass("disabled")) {
		return false;
	}
	sessionStorage.select = selectDatas[0].id;
	updateData();
}
function updateData() {
	indicatorExtractionDialog.iframeDialog(requestPath + "/yddemo/indicatorDataExtractions/edit",
			indicatorExtractionLocale.text.edit, 450, 340, "yddemo:indicatorDataExtraction:edit", false);
}

/** 删除 * */
function deleteIndicatorDataExtraction() {
	if ($("[action='yddemo:indicatorDataExtraction:delete']").hasClass("disabled")) {
		return false;
	}
	deleteData();
}
function deleteData() {
	indicatorExtractionDialog.confirmDialog(indicatorExtractionLocale.text.isDelete, function() {
		var selectDatas = indicatorExtractionTable.getSelectDatas();
		var dataList = [];
		for (var i = 0; i < selectDatas.length; i++) {
			dataList.push(selectDatas[i].id);
		}
		var resultJson = indicatorExtractionAjax.sendAjax({
			url : requestPath + "/yddemo/indicatorDataExtractions/", // 请求url*
			params : { // 请求url参数(非必配)
				_method : "DELETE"
			},
			async : false, // 是否异步*
			type : "POST", // 请求类型*
			data : dataList
		});
		if (resultJson.success) {
			indicatorExtractionDialog.tipDialog(indicatorExtractionLocale.text.deleteSuccess,
					function() {
						// pageQuery(); // 删除成功后,刷新页面
						indicatorExtractionTable.reloadData();
						disableButton();
					});
		} else {
			indicatorExtractionDialog.alertDialog(resultJson.message); // 删除失败后,弹出警告框
		}
	});
}

/**
 * 窗口大小改变时触发
 * 
 * @param
 * @return
 */
$(window).resize(function() {
	resetTableHeight("indicatorDataExtractionTableDiv", array);
});
